﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MatchResults.Services;
using System.Threading;
using System.Windows.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace MatchResults.View
{
    public partial class LiveResultView : UserControl
    {
        DispatcherTimer timer;
        public LiveResultView()
        {

            InitializeComponent();
            if (App.LiveResultModel == null)
            {
                App.LiveResultModel = new List<ViewModel.LiveMatchResultViewModel>();

            }
            grdMatchResult.ItemsSource = App.LiveResultModel;
            List<ViewModel.LiveMatchResultViewModel> temp;
            LiveMatchResultService reader = new LiveMatchResultService();
            reader.OnComplete += new EventHandler(reader_OnComplete);

            if (prgssLoading != null)
            {
                txtLoading.Visibility = System.Windows.Visibility.Visible;
                prgssLoading.Visibility = System.Windows.Visibility.Visible;
                prgssLoading.IsIndeterminate = true;
            }
            reader.GetMessages(messages => temp = messages.ToList(), string.Empty);

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(30);
            timer.Tick += new EventHandler(GetLiveResult);

            timer.Start();
        }

        void GetLiveResult(object sender, EventArgs e)
        {
            List<ViewModel.LiveMatchResultViewModel> temp;

            LiveMatchResultService reader = new LiveMatchResultService();
            reader.OnComplete += new EventHandler(reader_OnComplete);

            if (prgssLoading != null)
            {
                txtLoading.Visibility = System.Windows.Visibility.Visible;
                prgssLoading.Visibility = System.Windows.Visibility.Visible;
                prgssLoading.IsIndeterminate = true;

            }

            reader.GetMessages(messages => temp = messages.ToList(), string.Empty);
        }

        void reader_OnComplete(object sender, EventArgs e)
        {
            if (App.LiveResultModel.Any(c=>!string.IsNullOrEmpty(c.LeftGole1))
                ||
                App.LiveResultModel.Any(c=>!string.IsNullOrEmpty(c.RightGole1))
                     ||
                App.LiveResultModel.Any(c=>!string.IsNullOrEmpty(c.LeftGole2))
                     ||
                App.LiveResultModel.Any(c => !string.IsNullOrEmpty(c.RightGole2))
                )
            {
                PlaySound(@"Sounds\goal.wav");
            }
            if (prgssLoading != null)
            {
                prgssLoading.IsIndeterminate = false;
                txtLoading.Visibility = System.Windows.Visibility.Collapsed;
                prgssLoading.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void PlaySound(string path)
        {
            try
            {
                if (!string.IsNullOrEmpty(path))
                {
                    using (var stream = TitleContainer.OpenStream(path))
                    {
                        if (stream != null)
                        {
                            var effect = SoundEffect.FromStream(stream);
                            FrameworkDispatcher.Update();
                            effect.Play();
                        }
                    }
                }
            }
            catch { }
        }


    }
}
